<%!

  Copyright (c) Facebook, Inc. and its affiliates.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

%><% > Autogen%>
#pragma once

<%#program:thrift_includes%>
#include  "<%program:include_prefix%><%program:name%>_fatal_types.h"
<%/program:thrift_includes%>

#include "<%program:include_prefix%><%program:name%>_types.h"
#include "<%program:include_prefix%><%program:name%>_fatal.h"

#include <fatal/type/enum.h>
#include <fatal/type/variant_traits.h>

#include <type_traits>

<% > common/namespace_cpp2_begin%>


namespace __fbthrift_refl {
namespace __fbthrift_refl_impl = ::apache::thrift::detail::reflection_impl;
<%#program:structs%><%#struct:union?%>
class <%struct:name%>_tenum_traits {
 public:
  using type = <% > common/namespace_cpp2%><%struct:name%>::Type;

 private:
  <%#struct:fields%>
  struct __fbthrift_value_<%field:cpp_name%> {
    using name = __fbthrift_strings_<%program:name%>::<%field:name_hash%>;
    using value = std::integral_constant<type, type::<%field:cpp_name%>>;
    <%#field:fatal_annotations?%>
    class annotations {
      struct __fbthrift_keys {
        <%#field:fatal_annotations%>
        using <%annotation:safe_key%> = __fbthrift_strings_<%program:name%>::<%annotation:safe_key%>;
        <%/field:fatal_annotations%>
      };

      struct __fbthrift_values {
        <%#field:fatal_annotations%>
        using <%annotation:safe_key%> = <%annotation:fatal_string%>;
        <%/field:fatal_annotations%>
      };

     public:
      using keys = __fbthrift_keys;
      using values = __fbthrift_values;
      using map = ::fatal::list<
          <%#field:fatal_annotations%>
          ::apache::thrift::annotation<keys::<%annotation:safe_key%>, values::<%annotation:safe_key%>><%^last?%>,<%/last?%>
          <%/field:fatal_annotations%>
      >;
    };
    <%/field:fatal_annotations?%>
    <%^field:fatal_annotations?%>
    using annotations = __fbthrift_refl_impl::no_annotations;
    <%/field:fatal_annotations?%>
  };

  <%/struct:fields%>
  struct __fbthrift_member {
    <%#struct:fields%>
    using <%field:cpp_name%> = __fbthrift_value_<%field:cpp_name%>;
    <%/struct:fields%>
  };

 public:
  using name = __fbthrift_strings_<%program:name%>::Type;
  using member = __fbthrift_member;
  using fields = ::fatal::list<
      <%#struct:fields%>
      member::<%field:cpp_name%><%^last?%>,<%/last?%>
      <%/struct:fields%>
  >;

  using annotations = __fbthrift_refl_impl::no_annotations;

  static char const *to_string(type e, char const *fallback) {
    switch (e) {
      <%#struct:fields%>
      case type::<%field:cpp_name%>: return "<%field:cpp_name%>";
      <%/struct:fields%>
      default: return fallback;
    }
  }
};

class <%struct:name%>_variant_traits {
 private:
  struct __fbthrift_ids {
    <%#struct:fields%>
    using <%field:cpp_name%> = std::integral_constant<<% > common/namespace_cpp2%><%struct:name%>::Type, <% > common/namespace_cpp2%><%struct:name%>::Type::<%field:cpp_name%>>;
    <%/struct:fields%>
  };

  <%#struct:fields%>
  struct __fbthrift_member_<%field:cpp_name%> {
    using name = __fbthrift_strings_<%program:name%>::<%field:name_hash%>;
    static constexpr ::apache::thrift::field_id_t id = <%field:key%>;
    using type_class = <%#field:type%><%type:type_class%><%/field:type%>;
    struct setter {
      template <typename... Args>
      decltype(auto) operator ()(<% > common/namespace_cpp2%><%struct:name%> &variant, Args &&...args) const {
        return variant.set_<%field:cpp_name%>(std::forward<Args>(args)...);
      }
    };
    struct getter {
      decltype(auto) operator ()(<% > common/namespace_cpp2%><%struct:name%> const &variant) const {
        return variant.get_<%field:cpp_name%>();
      }
      decltype(auto) operator ()(<% > common/namespace_cpp2%><%struct:name%> &variant) const {
        return variant.mutable_<%field:cpp_name%>();
      }
      decltype(auto) operator ()(<% > common/namespace_cpp2%><%struct:name%> &&variant) const {
        return std::move(variant).move_<%field:cpp_name%>();
      }
    };
  };
  <%/struct:fields%>

 public:
  using type = <% > common/namespace_cpp2%><%struct:name%>;
  using name = __fbthrift_strings_<%program:name%>::<%struct:name%>;
  using id = type::Type;
  using ids = __fbthrift_ids;
  using descriptors = ::fatal::list<
      <%#struct:fields%>
      ::fatal::variant_member_descriptor<
          <%#field:type%><%type:cpp_type%><%/field:type%>,
           __fbthrift_ids::<%field:cpp_name%>,
          __fbthrift_member_<%field:cpp_name%>::getter,
          __fbthrift_member_<%field:cpp_name%>::setter,
          ::apache::thrift::reflected_variant_member_metadata<__fbthrift_member_<%field:cpp_name%>>><%^last?%>,<%/last?%>
    <%/struct:fields%>
  >;

  <%#struct:fatal_annotations?%>
  class annotations {
    struct __fbthrift_keys {
      <%#struct:fatal_annotations%>
      using <%annotation:safe_key%> = __fbthrift_strings_<%program:name%>::<%annotation:safe_key%>;
      <%/struct:fatal_annotations%>
    };

    struct __fbthrift_values {
      <%#struct:fatal_annotations%>
      using <%annotation:safe_key%> = <%annotation:fatal_string%>;
      <%/struct:fatal_annotations%>
    };

   public:
    using keys = __fbthrift_keys;
    using values = __fbthrift_values;
    using map = ::fatal::list<
        <%#struct:fatal_annotations%>
        ::apache::thrift::annotation<keys::<%annotation:safe_key%>, values::<%annotation:safe_key%>><%^last?%>,<%/last?%>
        <%/struct:fatal_annotations%>
    >;
  };
  <%/struct:fatal_annotations?%>
  <%^struct:fatal_annotations?%>
  using annotations = __fbthrift_refl_impl::no_annotations;
  <%/struct:fatal_annotations?%>

  static id get_id(type const &variant) {
    return variant.getType();
  }

  static bool empty(type const &variant) {
    return variant.getType() == id::__EMPTY__;
  }

  static void clear(type &variant) {
    return variant.__clear();
  }
};
<%/struct:union?%><%/program:structs%>
} // __fbthrift_refl

<%#program:structs%><%#struct:union?%>
FATAL_REGISTER_ENUM_TRAITS(
  __fbthrift_refl::<%struct:name%>_tenum_traits,
  ::apache::thrift::detail::type_common_metadata_impl<
      <%program:name%>_tags::module,
      ::apache::thrift::reflected_annotations<__fbthrift_refl::<%struct:name%>_tenum_traits::annotations>,
      static_cast<::apache::thrift::legacy_type_id_t>(0ull)
  >
);
FATAL_REGISTER_VARIANT_TRAITS(
  __fbthrift_refl::<%struct:name%>_variant_traits,
  ::apache::thrift::detail::type_common_metadata_impl<
      <%program:name%>_tags::module,
      ::apache::thrift::reflected_annotations<__fbthrift_refl::<%struct:name%>_variant_traits::annotations>,
      static_cast<::apache::thrift::legacy_type_id_t>(<%struct:legacy_type_id%>ull)
  >
);
<%/struct:union?%><%/program:structs%>
<% > common/namespace_cpp2_end%>
